/**
 * Created by Administrator on 2017/3/13.
 */
//$("#footer").load("common/footer.html");
var page=1;//下拉加载的页码
var cid='';//分类id
$(function() {
    //判断是否有搜索内容
    var searchTxt=getQueryString('keyword');
    if(searchTxt){
        //$('.sliderImage,.fenleiBImg,.fenleiSImg').css('display','none');

        $('#searchText span').html(searchTxt);
        ajaxGoodsList(1, 6, '','',searchTxt);
    }else {
        //加载轮播图
        $.get(commonURL + 'op=banner', function (data) {
            var res = JSON.parse(data);
            var liHtml = '';
            var numHtml = '';
            for (var i = 0; i < res.length; i++) {
                liHtml += "<li><img src=" + res[i].thumb + " style='width:100%;'></li>";
                if (i == 0) {
                    numHtml += '<span class="curr"></span>'
                } else {
                    numHtml += '<span></span>'
                }
            }
            $('#sliderImg>ul').html(liHtml);
            $('#sliderNum').html(numHtml);

            new Swipe($('#sliderImg')[0], {
                speed: 500,
                auto: 3000,
                callback: function () {
                    var lis = $("#sliderNum").children();
                    lis.removeClass("curr").eq(this.index).addClass("curr");
                }
            });
        });

        //加载商品分类图
        $.get(commonURL + 'op=category', function (data) {
            var res = JSON.parse(data);
            console.log(res);
            var liHtmlS = '',liHtmlB = '';
            for (var i = 0; i < res.length; i++) {
                if(res[i].show_type=='1'){
                    liHtmlB += '<a onclick=fenleiSImg(' + res[i].id + ',this) href="##"><img src=' + res[i].smallthumb + ' alt=""/></a>';
                }else{
                    liHtmlS += '<a onclick=fenleiSImg(' + res[i].id + ',this) href="##"><img src=' + res[i].thumb + ' alt=""/><span>' + res[i].name + '</span></a>';
                }
            }
            if(liHtmlB==''){
                $('.fenleiBImg').css('display','none');
            }
            if(liHtmlS==''){
                $('.fenleiSImg').css('display','none');
            }
            $('.fenleiSImg').html(liHtmlS);
            $('.fenleiBImg').html(liHtmlB);
        });

        //加载首页产品
        ajaxGoodsList(1, 6, cid);
    }
});

//弹窗
$('.top li:nth-child(2)').click(function(e){
    e.stopPropagation(e);//阻止冒泡
    //$('.top li:nth-child(2)>a').css('transform','rotate(90deg)');
    //$('.sortlight').css('display','block');
    $('.sortlight').slideToggle();
});
document.onclick=function(){
    $('.top li:nth-child(2)>a').css('transform','rotate(0deg)')
    $('.sortlight').css('display','none');
};

//滚动加载
$(document.body).infinite();//初始化
var loading = false;  //状态标记
$(document.body).infinite().on("infinite", function() {
    if(loading) return;
    page++;
    loading = true;
    $('.loadready').html('正在玩命加载中...');
    setTimeout(function() {
        ajaxGoodsList(page,6,cid,'append');
        loading = false;
        $('.loadready').html('');
    }, 700);   //模拟延迟
});

//$(document.body).destroyInfinite();//停止加载


//搜索
$('.searchSucc').click(function(){
    var searchTxt=$('#searchInput').val();
    if(searchTxt==""){
        return false;
    }
    location.href=searchUrl+"keyword="+searchTxt;
});

//分类小图
function fenleiSImg(id,self){
    cid=id;
    if($(self).find('span').hasClass('active')){return false;}
    $(document.body).infinite();//初始化
    $('.fenleiSImg span').removeClass('active');
    $(self).children('span').addClass('active');
    page=1;
    ajaxGoodsList(page,6,cid);
}

//跳转到详情页
function gotoDetail(self){
    var id=$(self).attr('data-id');
    location.href=detailurl+"id="+id;
}

//加载商品列表
function ajaxGoodsList(pagenum,pagesize,cid,append,keyword){
    $.get(
        commonURL+'op=goods_list',
        {
            page:pagenum,
            pagesize:pagesize,
            cid:cid,
            keyword:keyword
        },
        function(data){
            var res=JSON.parse(data);
            console.log(res);
            var listHtml='';
            for(var i=0;i<res.list.length;i++){
                var deliverytype=res.list[i].deliverytype=='1'?'上门':'到店';
                listHtml+='<div class="lists_data"><div class="data_img"><img onclick="gotoDetail(this)" data-option='+res.list[i].hasoption+' data-id='+res.list[i].id+' src='+res.list[i].gimg+' alt=""/></div>'
                +'<div><div class="data_title"><b>'+res.list[i].gname+'</b></div>'
                +'<div class="data_price">￥<b>'+res.list[i].oprice+'</b><s>￥'+res.list[i].mprice+'</s>'
                +'<i class="iconal-yuebiao" onclick="yuebiaoClick(this,event)" data-id='+res.list[i].id+' data-type='+res.list[i].deliverytype+' data-params='+JSON.stringify(res.list[i].params)+'></i>'
                +'</div><div class="data_time"><ul><li class="data-type"><span>'+deliverytype+'</span></li><li style="width: 5em"><i class="iconal-zuyue"></i><span>'+res.list[i].endtime+'</span>分钟</li>'
                +'<li><i class="iconal-qunzuduoren"></i><span>'+res.list[i].salenum+'人做过</span></li></ul></div></div></div>'
            }
            if(append=='append'){
                $(".lists").append(listHtml);
            }else {
                $(".lists").html(listHtml);
            }
            if(res.list.length<1){
                //$.toptip('全部完毕', 'success');
                //$.toast('数据加载完毕','success');
                $('.loadready').html('数据加载完毕').css('color','#aaa');
                $(document.body).destroyInfinite();//停止加载
                return false;
            }
        }
    )
}













